package com.dp.service.impl3;

import java.io.File;
import java.util.List;

import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dp.pojo.JsonPage;
import com.dp.util.DataMap;
import com.dp.vo.NewsPlatforminfo;
import com.dp.vo.NewsUserupload;

@Service
@Transactional
public class UseruploadServiceImpl3 extends BaseImpl {
	
	/**
	 * Find all the NewsUserupload Entity
	 * @param hashMap
	 * @param firstrow
	 * @param maxrow
	 * @return List<NewsUserupload>
	 */
	public List<NewsUserupload> findAllNewsUseruploadEntity(DataMap hashMap, Integer firstrow,
			Integer maxrow){
		NewsUserupload userupload=(NewsUserupload) hashMap.get("newsUserupload");
		List<NewsUserupload> queryList=null;
		JsonPage jpage = (JsonPage) hashMap.get("jpage");
		String clientid = hashMap.getMapData("clientid");
		String usersetname = hashMap.getMapData("usersetname");
		String isok = hashMap.getMapData("isok");
		String ischeck = hashMap.getMapData("ischeck");
		try {
			StringBuffer sf = new StringBuffer("SELECT n FROM NewsUserupload n WHERE 1=1 ");
			logger.info("userupload"+(userupload!=null));
//			if(userupload!=null){
//				logger.info("userupload:"+(userupload!=null));
//				logger.info("clientid:"+userupload.getClientid());
				if(clientid!=null&& clientid.trim().length() > 0){
					logger.info("clientid!=null");
					sf.append(" AND n.clientid='"
							+ clientid.trim() + "'");
				}
				if(usersetname!=null&& usersetname.trim().length() > 0){
					logger.info("usersetname!=null");
					sf.append(" AND n.usersetname like '%"
							+ usersetname.trim() + "%'");
				}
				if(isok!=null&& isok.trim().length() > 0){
					logger.info("isok!=null");
					sf.append(" AND n.isok like '%"
							+ isok.trim() + "%'");
				}
				if(ischeck!=null&& ischeck.trim().length() > 0){
					logger.info("userupload.getIscheck()!=null");
					sf.append(" AND n.ischeck like '%"
							+ ischeck.trim() + "%'");
				}
			//}
			sf.append(" AND isdeletebyuser!=2 AND isdelete!=2");
			if(jpage!=null){
				if(jpage.getSidx()!=null&&!"".equals(jpage.getSidx())){
					sf.append(" order by n."+jpage.getSidx());	
				    sf.append(" "+jpage.getSord());
				}
			}
			logger.info("sf:"+sf.toString());
			Query query = em.createQuery(sf.toString());
			if (firstrow != null) {
				query.setFirstResult(firstrow);
			}
			if (maxrow != null) {
				query.setMaxResults(maxrow);
			}
			queryList = query.getResultList();
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return queryList;
	}

	/**
	 * findNewsUseruploadById
	 * @param fileid
	 * @return	NewsUserupload
	 */
	public NewsUserupload findNewsUseruploadById(Integer fileid) {
		NewsUserupload o = null;
		try {
			o = this.em.find(NewsUserupload.class, fileid);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		if (o == null) {
			String msg = "Uh oh, '" + NewsPlatforminfo.class + "' object with id '" + fileid
					+ "' not found...";
			logger.warn(msg);
			throw new EntityNotFoundException(msg);
		}
	
		return o;
	}

	/**
	 * Ischecked
	 * Update set ischeck=1     	
	 * @param NewsPlatforminfo
	 * @return Boolean
	 */
	public Boolean Ischecked(NewsUserupload userupload) {
		Boolean resultb=true;
		try {
			String	jpql="UPDATE NewsUserupload n SET n.ischeck=:ischeck WHERE n.fileid=:fileid";
			Query	query = em.createQuery(jpql);
			query.setParameter("ischeck", userupload.getIscheck());
			query.setParameter("fileid", userupload.getFileid());
			int iCode=query.executeUpdate();
			if(iCode<0){
					resultb=false;
			}
		} catch (Exception e) {
			logger.warn(e.getMessage());
			e.printStackTrace();
			resultb=false;
		}
		return resultb;
	}

	
	public boolean deleteNewsUseruploadByFileID(int fileid) {
		List<NewsUserupload> queryList=null;
		NewsUserupload userupload = new NewsUserupload();
		boolean b = false;
		try {
			String sql = "SELECT n FROM NewsUserupload n WHERE n.fileid=" + fileid;
			logger.info("sql:"+sql);
			Query query = em.createQuery(sql);
			
			queryList = query.getResultList();
			logger.info("queryList.size()="+queryList.size());
			userupload = queryList.get(0);
			String filepath = userupload.getFullpath();   
			if(filepath!=null&&!"".equals(filepath)){
				Runtime rt = Runtime.getRuntime(); 
				File file = new File(filepath);   
				if(file.exists()){
					boolean flag  = file.delete();   
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		try {
			String jpql="UPDATE NewsUserupload n SET n.isdelete='2' WHERE n.fileid=:fileid";
			Query query = em.createQuery(jpql);
			query.setParameter("fileid", userupload.getFileid());
			int iCode=query.executeUpdate();
			if(iCode<0){
				b = false;
			}else{
				b = true;
			}
		} catch (Exception e) {
			logger.warn("deleteNewsPlatforminfoById fail !!!" + e.getMessage());
			e.printStackTrace();
		} 
		return b;
	}

}
